
package com.neegix.organization.psn.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.cqrs.command.UniversalCommandBus;
import com.neegix.cqrs.query.UniversalQueryBus;
import com.neegix.organization.psn.application.service.command.CreatePsnCommand;
import com.neegix.organization.psn.application.service.command.DeletePsnCommand;
import com.neegix.organization.psn.application.service.command.GenerateUsersCommand;
import com.neegix.organization.psn.application.service.command.UpdatePsnCommand;
import com.neegix.organization.psn.application.service.command.mapper.PsnCommandMapper;
import com.neegix.organization.psn.application.service.query.GetPagePsnQuery;
import com.neegix.organization.psn.application.service.query.GetPsnDetailQuery;
import com.neegix.organization.psn.application.service.query.mapper.PsnQueryMapper;
import com.neegix.organization.psn.interfaces.form.NewPsnForm;
import com.neegix.organization.psn.interfaces.form.QueryPsnForm;
import com.neegix.organization.psn.interfaces.form.UpdatePsnForm;
import com.neegix.organization.psn.interfaces.vo.PsnVO;
import com.neegix.inferfaces.result.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-11 15:13:32
  */

@RestController
@RequestMapping("/api/v1/psn")
public class PsnController {

    @Autowired
    private UniversalQueryBus queryBus;

    @Autowired
    private UniversalCommandBus commandBus;

    @PreAuthorize("hasAuthority('organization:psn:add')")
    @PostMapping
    public Result<Void> createPsn(@RequestBody @Valid NewPsnForm psnForm){
        CreatePsnCommand command = PsnCommandMapper.INSTANCE.covertCommand(psnForm);
        return Result.success("创建成功", commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('organization:psn:modify')")
    @PutMapping("/{id}")
    public Result<Void> updatePsn(@PathVariable Long id, @RequestBody @Valid UpdatePsnForm psnForm){
        UpdatePsnCommand command = PsnCommandMapper.INSTANCE.covertCommand(psnForm);
        command.setId(id);
        return Result.success("更新成功",commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('organization:psn:list')")
    @GetMapping({"/{currentPage}/{pageSize}/{pkBizUnit}","/{currentPage}/{pageSize}/{pkBizUnit}/{pkDept}"})
    public Result<PageVO<PsnVO>> getPsns(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize,@PathVariable("pkBizUnit") Long pkBizUnit, @PathVariable(name = "pkDept", required=false) Long pkDept, @ModelAttribute QueryPsnForm psnForm){
        if(psnForm == null) {
            psnForm = new QueryPsnForm();
        }
        GetPagePsnQuery query = PsnQueryMapper.INSTANCE.covertQuery(psnForm);
        query.setCurrentPage(currentPage);
        query.setPageSize(pageSize);
        query.setPkBizUnit(pkBizUnit);
        query.setPkDept(pkDept);
        return Result.success("查询成功",queryBus.execute(query));
    }

    @PreAuthorize("hasAuthority('organization:psn:get')")
    @GetMapping("/{id}")
    public Result<PsnVO> getPsnById(@PathVariable("id") Long id) {
        return Result.success("获取成功", queryBus.execute(new GetPsnDetailQuery(id)));
    }

    @PreAuthorize("hasAuthority('organization:psn:remove')")
    @DeleteMapping
    public Result<Void> removePsn(@RequestBody List<Long> ids){
        return Result.success("删除成功", commandBus.execute(new DeletePsnCommand(ids)));
    }

    @PostMapping("/batch-users")
     public Result<Void> batchUsers(@RequestBody List<Long> ids) {
        commandBus.execute(new GenerateUsersCommand(ids));
        return Result.success();
    }
}